<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/7/11
 * Time: 22:50
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class XKYPayment extends RechargeBaseAbstract implements WAPable
{
    protected $gw_pay = 'http://xky.105kc.com/submit.php';
    protected $gw_query = 'http://xky.105kc.com/api.php';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['trade_status'] == 'TRADE_SUCCESS') return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $params = $data;
        unset($params['sign']);
        unset($params['sign_type']);
        ksort($params);
        $signStr = "";
        foreach ($params as $key => $param) {
            $signStr .= "{$key}={$param}&";
        }
        $signStr .= $this->getMchKey();
        if (strcasecmp($data['sign'], md5($signStr))) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['out_trade_no'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['money'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['trade_no'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::XKYUN;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::ALI => 'alipay',
            PaymentMap::WX => 'wxpay',
            PaymentMap::QQ => 'qqpay',
            PaymentMap::WX_WAP => 'wxpay',
            PaymentMap::ALI_WAP => 'alipay',
            PaymentMap::QQ_WAP => 'qqpay',
        ];

    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('act', 'order');
        $this->setParameter('pid', $this->getMchId());
        $this->setParameter('key', $this->getMchKey());
        $this->setParameter('out_trade_no', $rechargeOrder->plat_no);
        $recharge_type = $rechargeOrder->order_data['recharge_type'];
        if ($recharge_type == PaymentMap::ALI || $recharge_type == PaymentMap::ALI_WAP) {
            $this->setGwQuery('http:// 5666pay.com/api.php');
        }
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            $body = json_decode($res['body'], true);
            if ($body['code'] == 1) return true;
            return $body['msg'];
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() .'?'. http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $params = $this->getParameters();
        unset($params['sign_type']);
        ksort($params);
        $signStr = "";
        foreach ($params as $key => $param) {
            $signStr .= "{$key}={$param}&";
        }
        $signStr = substr($signStr, 0, -1);
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        $this->setParameter('pid', $this->getMchId());
        $this->setParameter('out_trade_no', $rechargeOrder->plat_no);
        if ($mchData['recharge_type'] == PaymentMap::ALI || $mchData['recharge_type'] == PaymentMap::ALI_WAP) {
            $this->setGwPay('http:// 5666pay.com/submit.php');
        }
        $this->setParameter('type', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('name', 'merchant_order_pay');
        $this->setParameter('money', $rechargeOrder->order_amt);
        $this->setParameter('sign_type', 'MD5');
        $this->setParameter('return_url', $this->getReturnUrl());
        $this->setParameter('notify_url', $this->getCallbackUrl());
        $this->setParameter('sign', $this->paySign());

    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() .'?'. http_build_query($this->getParameters()));
    }
}